<template>
  <div style="display: flex">
    <div :style="{width:(isCollapse ? '64px' : '208px')}">
      <!-- 左侧菜单栏 -->
      <el-menu
          background-color="#42b983"
          style="width: 100%;height: 100vh;"
          text-color="#fff"
          active-text-color="#ff0"
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened router
          @select="selectMenu">
        <el-row style="padding-top: 10px">
          <el-col :span="2">

          </el-col>
          <el-col :span="7">
            <el-avatar src="/img/logo.webp"
                       style="width: 50px;height: 50px;background-color: transparent"></el-avatar>
          </el-col>
          <el-col :span="isCollapse ? 0 : 13">
            <router-link to="/" style="color: #fff;text-decoration: none;
position: relative;top: 15px;font-weight: bold">
              智慧医疗预约
            </router-link>
          </el-col>
        </el-row>
        <el-sub-menu index="1">
          <template #title>
            <el-icon style="font-size: 21px">
              <Promotion/>
            </el-icon>
            <span>工作管理</span>
          </template>
          <el-menu-item index="/bigDep">科室管理</el-menu-item>
          <el-menu-item index="/schedules">工作安排</el-menu-item>
          <el-menu-item index="/application">请假申请</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="2">
          <template #title>
            <el-icon style="font-size: 21px">
              <Checked/>
            </el-icon>
            <span>预约管理</span>
          </template>
          <el-menu-item index="/doctor">医生信息</el-menu-item>
          <el-menu-item index="/registration">就诊情况</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="3">
          <template #title>
            <el-icon style="font-size: 21px">
              <UserFilled/>
            </el-icon>
            <span>账号管理</span>
          </template>
          <el-menu-item index="/staff">员工管理</el-menu-item>
          <el-menu-item index="/user">用户管理</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="4">
          <template #title>
            <el-icon style="font-size: 21px">
              <Checked/>
            </el-icon>
            <span>信息管理</span>
          </template>
          <el-menu-item index="/agreement">预约须知</el-menu-item>
          <el-menu-item index="/introduce">医院简介</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </div>
    <div :style="{width:(isCollapse ? 'calc(100% - 64px)' : 'calc(100% - 208px)')}">
      <!-- 右侧：顶部菜单栏 -->
      <el-header style="padding-top: 22px;height: 10vh;background-color: #fff">
        <el-row :gutter="10">
          <el-col :span="18">
            <el-icon size="25px" @click="changeColl" v-if="!isCollapse">
              <Fold/>
            </el-icon>
            <el-icon size="25px" @click="changeColl" v-else>
              <Expand/>
            </el-icon>
            <h2 style="display: inline;position: relative;left: 10px;bottom: 5px">{{breadcrumb}}</h2>
          </el-col>
          <el-col :span="1">
            <el-icon size="25">
              <Avatar/>
            </el-icon>
          </el-col>
          <el-col :span="5" style="position: relative">
            <el-dropdown trigger="click" style="position: absolute;left: 0;">
              <span style="font-size: 19px;font-weight: bold">
                {{ staffName }}
              <el-icon><CaretBottom/></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>修改密码</el-dropdown-item>
                  <el-dropdown-item @click="outLogin">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </el-col>
        </el-row>
      </el-header>
      <!-- 右侧：主体内容 -->
      <el-main style="height: 90vh;padding: 0;overflow-y:auto;">
        <router-view/>
      </el-main>
    </div>
  </div>
</template>

<script setup>

import {onMounted, ref} from "vue";
import router from "@/router";
import {ElMessage} from "element-plus";

// 控制左侧菜单栏折叠
const isCollapse = ref(false);
const changeColl = () => {
  isCollapse.value = !isCollapse.value
}

// 获取登录的用户名
const staffName = ref('')
// 退出登录方法
const outLogin = () => {
  localStorage.removeItem('staff');
  router.push("/login")
}


onMounted(()=>{
  if (!localStorage.getItem('staff')){
    router.push("/login")
    ElMessage.warning("请登录账号！")
  }else {
    staffName.value = getUser().name
  }
})

//定义一个键值对数组来维护面包屑值与页面的对应关系
let map = {
  "/bigDep": '科室管理',
  "/schedules": '工作安排',
  "/application": '请假申请',
  "/doctor": '医生信息',
  "/registration": '就诊情况',
  "/staff": '员工管理',
  "/user": '用户管理',
  "/agreement": '预约须知',
  "/introduce": '医院简介'
}
//定义一个变量用来保存面包屑导航的当前值
const breadcrumb = ref(
    JSON.parse(localStorage.getItem('breadCrumb')) || '科室管理');
//定义一个函数,只要选择了某一个菜单项,立即切换面包屑导航的值
const selectMenu = (index)=>{
  breadcrumb.value = map[index];
  //解决面包屑值刷新页面丢失的问题:每次选择完菜单项,将面包屑的值也随之更新到localStorage中
  localStorage.setItem('breadCrumb',JSON.stringify(breadcrumb.value));
}

</script>

<style scoped>

</style>